package com.yonyou.brigade.third_parties.fanscar.controller;

import com.alibaba.fastjson.JSONArray;
import com.sun.org.apache.regexp.internal.RESyntaxException;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBaseDocService;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.third_parties.fanscar.model.FanscarConfig;
import com.yonyou.brigade.third_parties.fanscar.model.FanscarJobGrade;
import com.yonyou.brigade.third_parties.fanscar.model.FanscarUser;
import com.yonyou.brigade.third_parties.fanscar.service.IFanscarDocService;
import com.yonyou.brigade.third_parties.fanscar.service.IFanscarPublicService;
import com.yonyou.ybzboot.starter.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * {@link} {@link RestController}
 * 泛嘉档案操作controller
 *
 * @author kangfw5
 * @since 2019-03-01
 */
@RestController
@RequestMapping("/fanscar")
public class FanscarDocController {
    @Autowired
    private IFanscarPublicService fanscarPublicService;

    @Autowired
    private IFanscarDocService fanscarDocService;

    @Autowired
    @Qualifier("baseDocServiceImpl")
    private IBaseDocService baseDocService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService channelService;

    @RequestMapping(path = "/token", method = RequestMethod.POST)
    public String getToken(String tenant, String org) {
        FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), org, new FanscarConfig());

        try {
            return fanscarPublicService.getToken(fanscarConfig);
        } catch (Exception e) {
            throw new CustomException("获取token失败");
        }
    }

    @RequestMapping(path = "/h5/token", method = RequestMethod.POST)
    public String getH5Token(String tenant, String org, String employeeId) {
        FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), org, new FanscarConfig());

        try {
            return fanscarPublicService.getH5Token(employeeId, fanscarConfig);
        } catch (Exception e) {
            throw new CustomException("获取H5token失败");
        }
    }

    @RequestMapping(path = "/createDept", method = RequestMethod.POST)
    public JSONArray createDept(String tenant, String org, @RequestBody List<Map<String, String>> departmentList) {
        JSONArray result = new JSONArray();
        FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), org, new FanscarConfig());
        BriChannel briChannel = channelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.FANSCAR.getName());
        if (BriChannel.SYNCORGDEPT == briChannel.getSyncOrgDept()) {
            result = fanscarDocService.createDepartment(departmentList, fanscarConfig);
        }
        return result;
    }

    @RequestMapping(path = "/updateDept", method = RequestMethod.POST)
    public JSONArray updateDept(String tenant, String org, @RequestBody List<Map<String, String>> departmentList) {
        JSONArray result = new JSONArray();
        FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), org, new FanscarConfig());
        BriChannel briChannel = channelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.FANSCAR.getName());
        if (BriChannel.SYNCORGDEPT == briChannel.getSyncOrgDept()) {
            result = fanscarDocService.updateDepartment(departmentList, fanscarConfig);
        }
        return result;
    }

    @RequestMapping(path = "/createJobGrade", method = RequestMethod.POST)
    public JSONArray createJobGrade(String tenant, String org, @RequestBody List<FanscarJobGrade> jobGradeList) {
        JSONArray result = new JSONArray();
        FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), org, new FanscarConfig());
        BriChannel briChannel = channelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.FANSCAR.getName());
        if (BriChannel.SYNCRANK == briChannel.getSyncOrgDept()) {
            result = fanscarDocService.createJobGrade(jobGradeList, fanscarConfig);
        }
        return result;
    }

    @RequestMapping(path = "/updateJobGrade", method = RequestMethod.POST)
    public JSONArray updateGobGrade(String tenant, String org, @RequestBody List<FanscarJobGrade> jobGradeList) {
        JSONArray result = new JSONArray();
        FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), org, new FanscarConfig());
        BriChannel briChannel = channelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.FANSCAR.getName());
        if (BriChannel.SYNCRANK == briChannel.getSyncOrgDept()) {
            result = fanscarDocService.updateJobGrade(jobGradeList, fanscarConfig);
        }
        return result;
    }

    @RequestMapping(path = "/createEmployee", method = RequestMethod.POST)
    public JSONArray createEmployee(String tenant, String org, @RequestBody List<FanscarUser> employeeList) {
        JSONArray result = new JSONArray();
        FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), org, new FanscarConfig());
        BriChannel briChannel = channelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.FANSCAR.getName());
        if (BriChannel.SYNCUSER == briChannel.getSyncOrgDept()) {
            result = fanscarDocService.createEmployee(employeeList, fanscarConfig);
        }
        return result;
    }

    @RequestMapping(path = "/updateEmployee", method = RequestMethod.POST)
    public JSONArray updateEmployee(String tenant, String org, @RequestBody List<FanscarUser> employeeList) {
        JSONArray result = new JSONArray();
        FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), org, new FanscarConfig());
        BriChannel briChannel = channelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.FANSCAR.getName());
        if (BriChannel.SYNCUSER == briChannel.getSyncOrgDept()) {
            result = fanscarDocService.updateEmployee(employeeList, fanscarConfig);
        }
        return result;
    }

    @PostMapping(path = "/base/test")
    public Object test(String tenant, String org) {
        List<Map<String, String>> dutyByTenantAndOrg = baseDocService.getDutyByTenantAndOrg(tenant, org);
        return dutyByTenantAndOrg;
    }
}
